//
//  YZCConsultCollectionViewCell.m
//  LOLerBox
//
//  Created by 杨智超 on 16/1/16.
//  Copyright © 2016年 蓝鸥科技. All rights reserved.
//

#import "YZCConsultCollectionViewCell.h"
#import "ViewController.h"
#import "YZCConsultCell.h"
#import "YZCConsultImageCell.h"
#import "YZCNewsWebViewViewController.h"
#import "YZCConsultModel.h"
#import "YZCAFNetwork.h"
#import "CycleImage.h" /* 轮播图封装 */
#import "YZCNewsWebViewViewController.h" /* 详情页 */
#import <UIImageView+WebCache.h>
@interface YZCConsultCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate, CycleImageDelegate>

@end


@implementation YZCConsultCollectionViewCell



/* 初始化 */
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]init];
        [self.contentView addSubview:self.tableView];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[YZCConsultCell class] forCellReuseIdentifier:@"consultCell"];
        [self.tableView registerClass:[YZCConsultImageCell class] forCellReuseIdentifier:@"consultImageCell"];
        
        /* tableView头部 */
        self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 150)];
        self.headView.backgroundColor = [UIColor redColor];
        self.tableView.tableHeaderView = self.headView;
        
       
        /* 获取数据 */
        self.bannerArr = [NSMutableArray array];
        [YZCAFNetwork getUrl:@"http://lol.zhangyoubao.com/apis/rest/ItemsService/ads?i_=A000004850FCE4&t_=1452601087454&p_=24707&v_=400704&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=21&cha=Anzogame&u_=19460314&modle_=SM-N9009" body:nil response:YZCJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
            NSArray *arr = resposeObject[@"data"];
            /* 获取轮播图数组 */
            for (NSDictionary *dic in arr) {
              
                NSString *cycleImage = [dic objectForKey:@"pic_ad_url"];
              
                [self.bannerArr addObject:cycleImage];
                
                
            }
            CycleImage *cycle = [CycleImage CycleViewWithArray:self.bannerArr frame:CGRectMake(0, 0, 375, 150) page:CGRectMake(275 / 2, 140, 100, 10)];
            /* 设置代理人 */
            cycle.deleagte = self;
            
           
            [self.headView addSubview:cycle];
           

        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"数据解析失败");
        }];
        

        
    }
    return self;
}
/* 代理方法 */
- (void)didClick {
    
}



/* 布局方法 */
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    self.tableView.frame = CGRectMake(0, 0, layoutAttributes.frame.size.width, layoutAttributes.frame.size.height);
    

}

/* set方法 */
- (void)setStr:(NSString *)str
{
    if (_str != str) {
        _str = str;
    }
    [self handleData];
}



/* 获取数据 */
- (void)handleData {
    [YZCAFNetwork getUrl:self.str body:nil response:YZCJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSArray *arr = resposeObject[@"data"];
        self.dataArr = [YZCConsultModel modelHandlerWithArray:arr];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"数据解析失败");
    }];
    
    
    

}




/* tableView协议方法 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
    
}

/* 设置cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.model = self.dataArr[indexPath.row];
        if (self.model.pic_url.length == 0) {
            
        YZCConsultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"consultCell" forIndexPath:indexPath];
        cell.titleLabel.text = self.model.title;
        cell.descLabel.text = self.model.desc;
        /* 时间戳转成时间 */
        NSTimeInterval time = [self.model.published doubleValue];
        NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:time];
        /* 实例化一个NSDataFormatter对象 */
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        /* 显示的时间格式 */
        time = - time;
        long temp = 0;
        if ((temp = temp / 60) < 24) {
            [dateFormatter setDateFormat:@"今天 HH:mm"];
        }else{
            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        }
        
        NSString *currentDateStr = [dateFormatter stringFromDate:detaildate];
        cell.publishedLabel.text = currentDateStr;
        [dateFormatter release];
        return cell;
    }else {
        YZCConsultImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"consultImageCell"forIndexPath:indexPath];
        cell.titleLabel.text = self.model.title;
        cell.descLabel.text = self.model.desc;
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.model.pic_url]];
        
        NSTimeInterval time = [self.model.published doubleValue];
        NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:time];
        /* 实例化一个NSDataFormatter对象 */
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        /* 显示的时间格式 */
        time = - time;
        long temp = 0;
        if ((temp = temp / 60) < 24) {
            [dateFormatter setDateFormat:@"今天 HH:mm"];
        }else{
            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        }
        
        NSString *currentDateStr = [dateFormatter stringFromDate:detaildate];
        cell.publishedLabel.text = currentDateStr;
        [dateFormatter release];
        return cell;
    
    }
    
     }

/* tableView点击方法 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YZCConsultModel *model = _dataArr[indexPath.row];
    
    self.block(model.userid);
   
    
}




/* cell高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    YZCConsultModel *m = self.dataArr[indexPath.row];
    if (m.pic_url.length == 0) {
        CGFloat h1 = [YZCConsultCell heightWithText:m.title];
        CGFloat h2 = [YZCConsultCell heightWithText:m.desc];
        return h1 + h2 + 50;
    }else {
       return 80;
    }
    
}

@end
